package leetcode.f1t100;

/**
 * 寻找有序数列插入目标值的下标
 * https://leetcode.com/problems/search-insert-position/
 * @Author lichongxiao
 * @Date 2022/10/12 13:53
 */
public class Q35_SearchInsertPosition {
    public int searchInsert(int[] nums, int target) {
        int length = nums.length,left = 0,right = length -1;
        if(nums[length-1]<target){
            return length;
        }
        while(left < right){
            int middle = (left + right) / 2;
            if(nums[middle] == target){
                return middle;
            }else if(nums[middle] < target){
                left = middle + 1;
            }else{
                right = middle;
            }
        }
        return right;
    }

    public static void main(String[] args) {
        System.out.println(new Q35_SearchInsertPosition().searchInsert(new int[]{1,3,5,6}, 2));
    }
}
